
from rest_framework import serializers
from goods.models import SKU

class CartSerialzier(serializers.Serializer):

    sku_id = serializers.CharField(label='sku_id',required=True)
    count = serializers.IntegerField(label='个数',required=True,min_value=1)
    selected = serializers.BooleanField(label='是否选中',required=False,default=True)


    def validate(self, attrs):

        # 判断商品是否存在
        try:
            sku = SKU.objects.get(pk=attrs['sku_id'])
        except SKU.DoesNotExist:
            raise serializers.ValidationError('商品不存在')
        # 判断一下库存
        if sku.stock < attrs['count']:
            raise serializers.ValidationError('库存不足')

        return attrs



class  CartSKUSerializer(serializers.ModelSerializer):
    count = serializers.IntegerField(label='数量')
    selected = serializers.BooleanField(label='是否勾选')

    class Meta:
        model = SKU
        fields = ('id', 'count', 'name', 'default_image_url', 'price', 'selected')





class CartDeleteSerializer(serializers.Serializer):

    sku_id = serializers.IntegerField(label='商品id',min_value=1)


    def validate(self, attrs):

        try:
            sku = SKU.objects.get(pk=attrs['sku_id'])

        except:
            raise serializers.ValidationError('商品不存在')

        return attrs










